<template>
  <div class="article-detail">
    <wd-navbar
      :title="titleCategory"
      class="article-header"
      light
    >
      <div
        slot="left"
        @click="goBack"
      >
        <wd-icon
          class="middle"
          name="arrow-left"
        />
      </div>
      <div
        slot="right"
        class="icon-collect"
      >
      </div>
    </wd-navbar>
    <div class="article-content">
      <div
        class="radio-cover"
        v-if="this.$route.query.category==8"
      >
        <img :src="cover">
        <div class="radio-content">
          <SoundWave></SoundWave>
          <i
            class="play-icon"
            :class="playStatus?'pause-icon':'play-icon'"
            @click="setPlayStatus"
          ></i>
          <SoundWave></SoundWave>
        </div>

      </div>
      <h2 class="title">{{title}}</h2>
      <div class="article-content-text">
        <p class="author">{{author}}</p>
        <p class="author">{{volume}}</p>
      </div>
      <div
        class="artcile-content-radio"
        v-if="this.$route.query.category==1&&articleArr.audio!=''"
        @click="setAudio"
      >
        <div class="radio-ipt">
          <span>有声阅读 | {{articleArr.anchor}} </span>
        </div>
        <p>{{durationStr}}</p>
      </div>
      <div
        class="content"
        v-html="content"
      >
      </div>
      <div
        class="content-edit"
        v-if="content"
      >
        <p>{{articleArr.json_content.editor}}</p>
        <div>{{articleArr.json_content.copyright}}</div>
      </div>
      <div class="content-authior-box">
        <div class="author">作者</div>
        <div class="author-list-box">
          <div class="author-list-container">
            <div class="author-list">
              <img
                :src="authorList.web_url"
                alt=""
              >
            </div>
            <div class="author-list-text">
              <p>{{authorList.user_name}}</p>
              <p class="txt">{{authorList.summary}}</p>
            </div>
          </div>
          <div class="attention">
            关注
          </div>
        </div>
      </div>
      <div class="comment-nav">评论列表</div>
      <div
        v-for="(c,i) in commentList"
        :key="i"
        class="comment-box"
      >
        <div class="comment-list">
          <div class="comment-item">
            <div class="pic">
              <img :src="c.user.web_url">
            </div>
            <span class="user-name">{{c.user.user_name}}</span>
          </div>
          <span class="time">{{c.input_date}}</span>
        </div>
        <div
          class="quote"
          v-if="c.quote"
        >
          {{c.quote}}
        </div>
        <span class="text">{{c.content}}</span>
      </div>
    </div>
    <audio
      v-if="audio"
      :src="audio"
      ref="audio"
      @canplay="getAudioMetaSource"
      @timeupdate="audioTimeupdate"
    />
  </div>
</template>

<script>
import { getArticleDetail } from "@/utils/apis/articleContent";
import { getArticleDetailComment } from "@/utils/apis/articleContent";
import SoundWave from "@/components/SoundWave.vue";
import { filterDuration } from "@/utils/dateTools";
export default {
  props: {
    popupShow: Boolean,
    src: String,
    playStatus: Boolean,
  },
  components: { SoundWave },
  name: "ArticleDetailView",

  data() {
    return {
      title: "", // 文章标题
      author: "", // 文章作者
      content: "", // 文章内容
      editor: "", // 编辑者
      volume: "", // 电台期数
      cover: "", // 电台图片
      audio: "", // 音频地址
      articleArr: [],
      authorList: [],
      commentList: [],
      time: "",
      titleCategory: "", // 文章标题类别
      contentId: "",
      category: "",
      duration: "",
      currentTime: "",
      audioPlayStatus:true, 
    };
  },
  activated() {
    let { id, category } = this.$route.query;
    if (this.contentId == id && this.category == category) {
      return;
    }

    this.getArticleDetailAndComment();
  },
  created() {
    this.getArticleDetailAndComment();
  },
  computed: {
    durationStr() {
      return filterDuration(this.duration);
    },
  },

  methods: {
    // 获取文章详情与评论函数
    getArticleDetailAndComment() {
      let { id, category } = this.$route.query;
      // 获取文章详情
      getArticleDetail({
        axios: this.$axios,
        id,
        category,
      }).then((res) => {
        this.articleArr = res.data.data;
        this.contentId = res.data.data.id;
        this.category = res.data.data.category;
        this.title = this.articleArr.json_content.title;
        this.author = this.articleArr.json_content.simple_author[0];
        this.volume = this.articleArr.json_content.simple_author[1];
        this.content = this.articleArr.json_content.content;
        this.authorList = this.articleArr.author_list[0];
        this.cover = this.articleArr.json_content.cover;
        this.audio = this.articleArr.audio;
        this.titleCategory = this.articleArr.share_list.wx.title.split("|");
        if (this.articleArr.category == "8") {
          this.titleCategory[0] = this.titleCategory[0].slice(3);
        }
        this.titleCategory = this.titleCategory[0];
        this.$nextTick(() => {
          if (this.category == "8" && this.audio) {
            let audio = this.articleArr.audio;
            let anchor = this.articleArr.author_list[0].user_name;
            let title = this.articleArr.json_content.title;
            console.log(anchor);
            this.$emit("change-popup-show", {
              audio,
              title,
              anchor,
              duration: this.duration,
              currentTime: this.currentTime,
              detailPath: this.$route.fullPath,
              audioPlayStatus:this.audioPlayStatus,
            });
          }
        });
      });
      // 获取评论详情
      getArticleDetailComment({
        axios: this.$axios,
        id,
        category,
      }).then((res) => {
        // console.log("网络请求---");
        this.commentList = res.data.data.data;
      });
    },
    // 点击左上角图标返回
    goBack() {
      this.$router.go(-1);
    },
    // 点击向父元素传值
    setAudio() {
      let audio = this.articleArr.audio;
      let anchor = this.articleArr.anchor;
      let title = this.articleArr.json_content.title;
      this.$emit("set-play-music", {
        audio,
        title,
        anchor,
        detailPath: this.$route.fullPath,
      });
    },
    // 获取audio数据
    getAudioMetaSource() {
      this.duration = this.$refs.audio.duration;
    },
    audioTimeupdate() {
      this.currentTime = this.$refs.audio.currentTime;
    },
    setPlayStatus(){
      this.$emit('set-play-status');
    }
  },
};
</script>

<style lang="scss" scoped>
.article-detail {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  // background: #ccc;
  overflow: auto;
  z-index: 555;
  background-color: #fff;

  .article-header {
    display: flex;
    align-items: center;
  }
  .icon-collect {
    position: relative;
    left: 62px;
    width: 20px;
    height: 20px;
    background-image: url("../../assets/img/article/favourite_gray.png");
    background-size: cover;
  }
  .wd-navbar.is-light::after {
    background: #cac3c3;
  }
  .article-content {
    min-width: 100vw;
    box-sizing: border-box;
    padding: 0 10px;
    .radio-cover {
      position: relative;
      img {
        width: 100%;
      }
      .radio-content {
        display: flex;
        width: 100%;
        position: absolute;
        bottom: 20px;
        left: 0;
        padding: 0 25px;
        box-sizing: border-box;

        .play-icon {
          display: block;
          width: 32px;
          height: 32px;
          margin: auto;
          background-image: url("@/assets/img/play/feeds_radio_play.png");
          background-size: cover;
        }
        .pause-icon {
          display: block;
          width: 32px;
          height: 32px;
          margin: auto;
          background-image: url("@/assets/img/play/feeds_radio_pause.png");
          background-size: cover;
        }
      }
    }
    .title {
      margin: 30px 0 40px;
      font-size: 24px;
      font-weight: bold;
      color: black;
    }
    .article-content-text {
      display: flex;
      justify-content: space-between;
      .author {
        font-size: 12px;
      }
    }
  }
  .artcile-content-radio {
    display: flex;
    justify-content: space-between;
    padding: 15px;
    margin-top: 30px;
    border: 1px solid rgb(163, 159, 159);
    border-radius: 5px;
    .radio-ipt {
    }
  }
  .content {
    font-size: 14px;
    line-height: 2;
  }
  .content-edit {
    margin: 30px 0;
    font-size: 12px;
    color: #a19898;

    p {
      margin-bottom: 15px;
    }
  }
  .content-authior-box {
    .author {
      width: 60px;
      padding: 10px 0;
      font-size: 14px;
      border-bottom: 5px solid black;
    }
    .author-list {
      display: flex;
      align-items: center;
      width: 40px;
      height: 40px;
      // margin: 10px 10px 0 0;
      margin-right: 10px;
      border-radius: 999px;
      overflow: hidden;
      img {
        width: 100%;
      }
    }
    .author-list-box {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 0;
      .author-list-container {
        display: flex;
        align-items: center;
      }
      .author-list-text > p {
        margin: 0;
        // line-height: 30px;
        padding: 5px 0;
        font-size: 14px;
      }
      .author-list-text {
        .txt {
          color: #a19898;
        }
      }
      .attention {
        flex: 0 0 38px;
        padding: 10px;
        border: 1px solid #a19898;
        border-radius: 3px;
        color: #a19898;
      }
    }
  }
  .comment-nav {
    width: 60px;
    padding: 10px 0;
    font-size: 14px;
    border-bottom: 5px solid black;
  }
  .comment-box {
    padding: 15px 0;
    border-bottom: 1px solid rgb(214, 204, 204);
    font-size: 14px;
    .comment-list {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
      margin-bottom: 10px;
      .time {
        font-size: 12px;
        color: #a19898;
      }
      .pic {
        margin-right: 5px;
        width: 20px;
        height: 20px;
        border-radius: 999px;
        overflow: hidden;

        img {
          width: 100%;
        }
      }
      .user-name {
        color: #a19898;
      }
      .comment-item {
        display: flex;
        align-items: center;
      }
    }
    .quote {
      margin: 10px 20px 15px;
      padding: 5px 10px;
      border: 1px solid #ccc;
      line-height: 1.5;
      // transform: translateX(20px);
      // box-sizing: border-box;
    }
    .text {
      display: inline-block;
      line-height: 1.5;
      padding: 0px 20px;
    }
  }
}
</style>